package cn.jdx.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("[拦截器]:开启全局域请求");
        //表示所有的域都可以接受
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许所有的方法
        response.setHeader("Access-Control-Allow-Methods","POST,GET,DELETE,PUT,OPTIONS");
        //请求得到结果的有效期
        response.setHeader("Access-Control-Max-Age","3600");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,Content-Type,access-control-allow-origin,version-info");
        //该值是一个boolean值,表示允许发送cookie，默认情况下cookie不包含在cores中，设置为true
        response.setHeader("Access-Control-Allow-Credentials","true");
        return true;
    }
}
